import 'package:bruno/bruno.dart';
import 'package:common/app_config.dart';
import 'package:common/common.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:keyboard_dismisser/keyboard_dismisser.dart';
import 'package:provider/provider.dart';

import 'language/translation_service.dart';
import 'module/app.dart';
import 'module/un_known_view.dart';
import 'router/app_router.dart';
import 'router/getx_router_observer.dart';
import 'router/qs_navigator_observer.dart';
import 'theme/theme_model.dart';

class TUIKitDemoApp extends StatelessWidget {
  TUIKitDemoApp({super.key}) {
    /// SmartDialog 配置
    smartDialogConfig();
  }
  @override
  Widget build(BuildContext context) {
    final theme = Provider.of<DefaultThemeData>(context).theme;
    return Builder(builder: (context) {
      return KeyboardDismisser(
        gestures: const [GestureType.onTap, GestureType.onPanUpdateDownDirection],
        child: ScreenUtilInit(
          designSize: const Size(375, 812),
          // minTextAdapt: true,
          // splitScreenMode: true,
          child: MediaQuery(
            data: MediaQuery.of(context).copyWith(textScaler: TextScaler.noScaling),
            child: GetMaterialApp(
              title: '喵趣',
              debugShowCheckedModeBanner: false,
              locale: TranslationService.locale,
              supportedLocales: const [Locale('zh', 'CH'), Locale('en', 'US')],
              localizationsDelegates: [
                ...GlobalMaterialLocalizations.delegates,
                BrnLocalizationDelegate.delegate,
              ],
              theme: ThemeData(
                platform: TargetPlatform.iOS,
                pageTransitionsTheme: const PageTransitionsTheme(builders: {
                  TargetPlatform.iOS: CupertinoPageTransitionsBuilder(),
                  TargetPlatform.android: CupertinoPageTransitionsBuilder(),
                }),
                elevatedButtonTheme:
                    ElevatedButtonThemeData(style: ElevatedButton.styleFrom(backgroundColor: theme.primaryColor)),
              ), //是否展示Log
              enableLog: AppConfig.isDebug,
              //异步依赖注入ApiService ApiRepository等对象
              // initialBinding: AppBinding(),
              //默认路由与路由表的加载
              initialRoute: CommonRouter.splash,
              getPages: AppRouter.routes,
              home: const TencentChatApp(),
              // 错误路由
              unknownRoute: GetPage(name: '/404', page: () => const UnKnownPage()),
              onUnknownRoute: (settings) {
                return MaterialPageRoute(builder: (context) {
                  return const UnKnownPage();
                });
              },
              builder: smartDialogBuilder(),
              defaultTransition: Transition.rightToLeft,
              smartManagement: SmartManagement.keepFactory,
              navigatorObservers: [
                GetXRouterObserver(),
                routeObserver,
                IMKitRouter.instance.routeObserver,
              ],
            ),
          ),
        ),
      );
    });
  }
}

/// SmartDialog初始化默认Loading与Toast
smartDialogBuilder() {
  return FlutterSmartDialog.init(
    builder: (BuildContext context, Widget? child) {
      return child!;
    },
    toastBuilder: (String msg) {
      return CustomToastWidget(msg: msg);
    },
    loadingBuilder: (String msg) {
      return CustomLoadingWidget(msg: msg == 'loading...' ? '加载中...'.tr : msg);
    },
    notifyStyle: FlutterSmartNotifyStyle(
      successBuilder: (String msg) => CustomSuccessWidget(msg: msg),
      failureBuilder: (String msg) => CustomFailureWidget(msg: msg),
      errorBuilder: (String msg) => CustomErrorWidget(msg: msg),
      alertBuilder: (String msg) => CustomErrorWidget(msg: msg),
      warningBuilder: (String msg) => CustomErrorWidget(msg: msg),
    ),
  );
}

/// SmartDialog 配置
void smartDialogConfig() {
  SmartDialog.config
    ..custom = SmartConfigCustom(
      maskColor: Colors.black.withOpacity(0.35),
      alignment: Alignment.center,
      useAnimation: true,
    )
    ..attach = SmartConfigAttach(
      animationType: SmartAnimationType.scale,
      usePenetrate: false,
    )
    ..loading = SmartConfigLoading(
      backType: SmartBackType.normal,
      clickMaskDismiss: true,
    )
    ..toast = SmartConfigToast(
      intervalTime: const Duration(milliseconds: 100),
      displayTime: const Duration(milliseconds: 2000),
      alignment: Alignment.center,
    );
}
